package priv.pattern.statemodel.example1;

import priv.pattern.statemodel.example1.abs.VedioState;
import priv.pattern.statemodel.example1.abs.impl.PauseState;
import priv.pattern.statemodel.example1.abs.impl.PlayState;
import priv.pattern.statemodel.example1.abs.impl.SpeedState;
import priv.pattern.statemodel.example1.abs.impl.StopState;

import java.util.HashMap;
import java.util.Map;

/*
 * 作者：m鹏h
 * 时间；2024/8/10 22:58
 * 描述：使用享元模式 , 共享同一个对象 ？？？？？
 * 上下文也有 play , pause , speed , stop 等状态
 *      执行这些方法时 , 调用状态的相应方法
 **/
public class VedioContext {
 private static final Map<String, VedioState> statePool = new HashMap<>();



 /**
  * 当前的状态
  */
 private VedioState mVedioState;
 //播放状态
 public final static PlayState PLAY_STATE = new PlayState();
 //暂停状态
 public final static PauseState PAUSE_STATE = new PauseState();
 //快进状态
 public final static SpeedState SPEED_STATE = new SpeedState();
 //停止状态
 public final static StopState STOP_STATE = new StopState();

 public VedioState getVedioState() {
  return mVedioState;
 }
/* public static VedioState getVedioState(String stateType) {
  VedioState state = statePool.get(stateType);
  if (state == null) {
   switch (stateType) {
    case "PLAY":
     state = new PlayState();
     break;
    case "PAUSE":
     state = new PauseState();
     break;
    case "SPEED":
     state = new SpeedState();
     break;
    case "STOP":
     state = new StopState();
     break;
   }
   statePool.put(stateType, state);
  }
  return state;
 }*/

 /**
  * 将传入的 VedioState , 赋值给当前的 VedioState mVedioState 成员
  *      除此之外 , 还要设置 VedioState 的上下文 , 即该类本身
  *      将当前的环境 VedioContext , 通知到各个状态实现类
  */
 public void setVedioState(VedioState mVedioState) {
  this.mVedioState = mVedioState;
  this.mVedioState.setVedioContext(this);
 }

 public void play() {
  this.mVedioState.play();
 }

 public void pause() {
  this.mVedioState.pause();
 }

 public void speed() {
  this.mVedioState.speed();
 }

 public void stop() {
  this.mVedioState.stop();
 }
}

